﻿using UnityEngine;
using System.Collections;
//497690484
public class No1_plane1_logic : MonoBehaviour {
	
	public ShootController gun;
	public float shootFrequency = 10;
	private float elaspedTimeSinceLastShoot;

	public float journeyTime = 60.0f;
	public float rotationSpeed = 0.0f;

	private Vector3 startPosition;
	private Vector3 endPosition;
	private float startTime;
	
	private bool bLogic = false;
	// Use this for initialization
	void Start () {
		elaspedTimeSinceLastShoot = 0;
	}
	
	// Update is called once per frame
	void Update () {
		elaspedTimeSinceLastShoot += Time.deltaTime;
		if(elaspedTimeSinceLastShoot > 1.0/shootFrequency){
			gun.fire();
			elaspedTimeSinceLastShoot = 0;
		}
	}

	public void Logic_onEnterScreen(){
		startPosition = transform.position;
		endPosition = transform.position + new Vector3(2,0, 0);
		startTime = Time.time;
		bLogic = true;
	}

	void doLogic(){
		if(bLogic){
			Vector3 center = (startPosition + endPosition) * 0.5F;
			center -= new Vector3(0, 1, 0);
			Vector3 riseRelCenter = startPosition - center;
			Vector3 setRelCenter = endPosition - center;
			float fracComplete = (Time.time - startTime) / journeyTime;
			
//			transform.position = Vector3.Slerp(riseRelCenter, setRelCenter, fracComplete);
//			transform.position += center;
			
			GameObject target = GameObject.Find("Plane_Hero");
			float turnSpeed = 3.0f;
			Vector3 moveDirection = target.transform.position - transform.position;
			moveDirection.z = 0;
			moveDirection.Normalize();
			
			float targetAngle = Mathf.Atan2(moveDirection.y,moveDirection.x) *  Mathf.Rad2Deg + 90.0f;
			float slerpValue = turnSpeed * Time.deltaTime;
			transform.rotation = Quaternion.Slerp(transform.rotation, 
			                                      Quaternion.Euler( 0, 0, targetAngle ), 
			                                      slerpValue );
		} 

	}
}








